package com.bj58.ecdata.monitor.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/fix")
public class TrackDataFixController{
	Logger log = LoggerFactory.getLogger(TrackDataFixController.class);
//	@Autowired
//	private TrackDao track;
	
	@RequestMapping("/track/minute")
	@ResponseBody
	public String trackMinute(@RequestParam String beginTime, @RequestParam String endTime, @RequestParam String type, @RequestParam(required = false, defaultValue = "1") int interval) {
		return "";
	}
	
//	/**
//	 * 根据历史趋势，修复分钟数据
//	 * @param time ： yyyyMMdd-HH:mm:ss
//	 * @return
//	 */
//	public <T> String fixMinuteDataByOtherDay(String beginTimeStr, String endTimeStr, String type,int interval) {
//		try {
//			Stopwatch watch = Stopwatch.createStarted();
//			Date beginTime = DateUtils.parseDate(beginTimeStr, "yyyyMMdd-HH:mm:ss");
//			Date endTime = DateUtils.parseDate(endTimeStr, "yyyyMMdd-HH:mm:ss");
//			final long beginMin = DateUtils.truncateToMinute(beginTime.getTime());
//			final long endMin = DateUtils.truncateToMinute(endTime.getTime());
//			final long nowMin = DateUtils.getTruncatedMinute();
//			if((DateUtils.getTruncatedToday() - DateUtils.truncateToToday(beginMin - interval * Constant._1d)) > 14*Constant._1d){
//				return "can't fix the data,because the data have already been deleted!";
//			}
//			//redis中的分钟数据只有2天
//			if((nowMin - beginMin) >= Constant._1d){
//				reloaMinuteDataFromMysql(beginMin - interval * Constant._1d, endMin- interval *  Constant._1d, track);
//			}
//			//昨天该时间段内每分钟的数据
//			List<TrackModel> params = createModels(beginMin - interval * Constant._1d, endMin- interval *  Constant._1d, Constant._1m, Constant._1d);
//			List<DataPoint> anotherDayDatas = track.loadMinute(type, params, false);
//			
//			//今天起点数据
//			Long startPoint = (Long)track.loadMinute(type, Lists.newArrayList(track.createModel(beginMin)), false).get(0).getValue();
//			//昨天起点数据
//			Long startPoint_lastDay = (Long)anotherDayDatas.remove(0).getValue();
//			//比率
//			double rate = startPoint * 1.0 / startPoint_lastDay;
//			for(DataPoint curPoint : anotherDayDatas){
//				TrackModel model = new TrackModel();
//				model.setTime(curPoint.getTime() + interval * Constant._1d);
//				Double doubleVal = Double.valueOf(((Long)curPoint.getValue()) * rate);
//				Long newVal =doubleVal.longValue();
//				track.getFieldInfo(type).getWriteMethod().invoke(model, newVal);
//				track.resetMinuteMultiDimension(model, type);
//			}
//			
//			String result = String.format("fix %s data between %s and %s, time %s", track.getTableName(), DateUtils.dateFormat(beginMin), DateUtils.dateFormat(endMin), watch.stop().toString());
//			log.info(result);
//			return result;
//		} catch (ParseException e) {
//			return String.format("error time! format(%s)", "yyyyMMdd-HH:mm:ss");
//		} catch (Exception e) {
//			return ExceptionUtils.getStackTrace(e);
//		}
//	}
//	
//
//	/**
//	 * 重新载入分钟数据
//	 * @throws Exception 
//	 */
//	public <T> String reloaMinuteDataFromMysql(final long beginTime, long endTime, final BaseDao<T> dao) throws Exception {
//		try {
//			int count = 0;
//			for(long min=beginTime; min <= endTime; min += Constant._1m){
//				String minuteKey = StringUtils.joinWithUnderline(dao.getTableName(), "mdmap", "minute", min);
//				dao.delRedisKey(minuteKey);
//				log.info("del redis key:{}",minuteKey);
//				count++;
//			}
//			Stopwatch watch = Stopwatch.createStarted();
//			int total = 60000*count;
//			final int block = total / 10;
//			final CountDownLatch cdl = new CountDownLatch(10);
//			for (int i = 0; i < total; i = i + block) {
//				final int num = i;
//				new Thread(new Runnable() {
//					@Override
//					public void run() {
//						for (int j = 0; j < block; j = j + 2000) {
//							long time = beginTime + num + j;
//							T param = dao.createModel(time);
//							dao.fixMinuteData(param);
//						}
//						cdl.countDown();
//					}
//				}).start();
//
//			}
//			cdl.await();
//			String result = String.format("reloade %s data between %s and %s, time %s", track.getTableName(), DateUtils.dateFormat(beginTime), DateUtils.dateFormat(endTime), watch.stop().toString());
//			log.info(result);
//			return result;
//		} catch (Exception e) {
//			throw e;
//		}
//
//	}
//	
//	private List<TrackModel> createModels(long startTime, long endTime, long interval, long span) {
//		long minTime = endTime - span;
//		if (startTime == 0 || startTime < minTime) {
//			startTime = minTime;
//		}
//
//		List<TrackModel> models = Lists.newArrayList();
//		for (long i = startTime; i <= endTime; i += interval) {
//			TrackModel model = new TrackModel();
//			model.setTime(i);
//			models.add(model);
//		}
//		return models;
//	}

}



